local life $life

local ave_panels $ave_panels

local r $r
local deprate $deprate

local graphs 1

local KW_per_panel=$KW_per_panel
* https://news.energysage.com/how-many-solar-panels-do-i-need/
pause off

clear


use ../Data/CleanedData/tractData, clear

*drop state // _merge

*merge m:1 state_fips using  ../Data/CleanedData/StatePrices

** generate variables for "average" hh


gen unit_ben=sunit*`ave_panels'

replace unit_ben=sunit_cap if unit_ben>sunit_cap

gen kwh_ben=skwh*A_t * `ave_panels'

gen cost=estimate_intercept+`ave_panels'*estimate_panels
// deal with state caps
gen cost_ben=scost*cost
gen state_sub=statetaxcredit*cost
replace cost_ben=cost_ben- (state_sub-statemaxtaxcredit) if state_sub>statemaxtaxcredit

gen price_ben=price*A_t*`ave_panels'

// kwh per metersq per day - deep solar ave=4.25

gen A_t_deepsolar=(A_t/365)/(1.65*.992)

sum A_t*





*order unit_ben kwh_cost cost_cost price_ben

gen tot_ben=price_ben+kwh_ben-cost+cost_ben+unit_ben

gen sub_ben=kwh_ben+cost_ben+unit_ben


sum tot_ben price_ben kwh_ben cost cost_ben unit_ben




order tot_ben price_ben kwh_ben cost cost_ben unit_ben
sum tot_ben price_ben kwh_ben cost cost_ben unit_ben




gen ptAt=price*A_t

gen linstall=log(solar_system_count_residential)

gen install_perc=existing_installs_count/number_of_panels_total // google
// using deep solar defs
gen install_percDS1=solar_system_count_residential/number_of_panels_total
gen install_percDS2=total_panel_area_residential/number_of_panels_total
gen install_percDS_pop=solar_system_count_residential/population

gen install_percDS_all=solar_system_count/number_of_panels_total

gen linstall_percDS1=log(install_percDS1)
gen linstall_percDS_pop=log(install_percDS_pop)



sum install_percDS1 if number_of_panels_total<10
sum install_percDS1 if number_of_panels_total>0
sum install_percDS1 if number_of_panels_total>-1



pause


****** regressions
gen tot_ben1000=tot_ben/1000 // these are the units in 

reg linstall_percDS1 tot_ben1000 CollPerc_t Pol_t [aw=number_of_panels_total]

reg linstall_percDS1 tot_ben1000 CollPerc_t Pol_t Owner_t [aw=number_of_panels_total]

reg linstall tot_ben1000 CollPerc_t Pol_t Owner_t [aw=number_of_panels_total]



predict pred_linstall

correlate linstall_percDS1 pred_linstall [aw=number_of_panels_total]

correlate linstall_percDS1 CollPerc_t [aw=number_of_panels_total]

correlate linstall_percDS1 tot_ben1000 [aw=number_of_panels_total]

correlate linstall_percDS1 Pol_t [aw=number_of_panels_total]

*********Outreg

egen regiong=group(region)
egen divisiong=group(division)

foreach var in price A_t skwh cost scost sunit CollPerc_t Pol_t {
	qui sum `var'
	gen `var'_std=`var'/r(sd)
}


gen panels_install=M_t/BI_t

sum panels_install


reg panels_install tot_ben1000  [aw=number_of_panels_total], r

corr price_ben sub_ben


local weight  number_of_panels_total // population // number_of_panels_total
foreach rhs in linstall linstall_percDS1  linstall_percDS_pop panels_install    { // install_percDS_pop  linstall  {

reg `rhs' tot_ben1000  [aw=`weight'], vce(cluster state_fips)

outreg2 using StataGraphs/install_reg`rhs', replace ctitle(Model 1) tex(frag) keep(tot_ben1000 ) nocons /// keep(tot_ben1000 )
addtext(Demographic Controls, NO, Region FE, NO, Division FE, NO)

sleep 10

reg `rhs' tot_ben1000 CollPerc_t Pol_t [aw=`weight'],  vce(cluster state_fips)


 
outreg2 using StataGraphs/install_reg`rhs', append ctitle(Model 2) tex(frag) keep(tot_ben1000 CollPerc_t Pol_t)  nocons /// keep(tot_ben1000 )
addtext(Demographic Controls, YES, Region FE, NO, Division FE, NO)

sleep 10

reg `rhs' tot_ben1000  i.regiong [aw=`weight'],  vce(cluster state_fips)
 
outreg2 using StataGraphs/install_reg`rhs', append ctitle(Model 3) tex(frag) keep(tot_ben1000 )  nocons /// keep(tot_ben1000 )
addtext(Demographic Controls, NO, Region FE, YES, Division FE, NO)

sleep 10

reg `rhs' tot_ben1000 CollPerc_t Pol_t i.regiong [aw=`weight'],  vce(cluster state_fips)

 
outreg2 using StataGraphs/install_reg`rhs', append ctitle(Model 3) tex(frag) keep(tot_ben1000 CollPerc_t Pol_t) nocons /// keep(tot_ben1000 )
addtext(Demographic Controls, YES, Region FE, YES, Division FE, NO)

sleep 10


reg `rhs' tot_ben1000  i.divisiong [aw=`weight'],  vce(cluster state_fips)
 
outreg2 using StataGraphs/install_reg`rhs', append ctitle(Model 3) tex(frag) keep(tot_ben1000 ) nocons /// keep(tot_ben1000 )
addtext(Demographic Controls, NO, Region FE, NO, Division FE, YES)
 
 sleep 10

reg `rhs' tot_ben1000 CollPerc_t Pol_t i.divisiong [aw=`weight'],  vce(cluster state_fips)
 
outreg2 using StataGraphs/install_reg`rhs', append ctitle(Model 3) tex(frag) keep(tot_ben1000 CollPerc_t Pol_t) nocons /// keep(tot_ben1000 )
addtext(Demographic Controls, YES, Region FE, NO, Division FE, YES)

sleep 10


reg `rhs'  price_ben sub_ben  cost [aw=`weight'],  vce(cluster state_fips)

outreg2 using StataGraphs/price_sub_reg_tract`rhs', replace  tex(frag)  keep(price_ben sub_ben cost) nocons addtext(Demographic Controls, NO)

sleep 10

reg `rhs'  price_ben sub_ben  cost CollPerc_t Pol_t  [aw=`weight'],  vce(cluster state_fips)

outreg2 using StataGraphs/price_sub_reg_tract`rhs', append  tex(frag)  keep(price_ben sub_ben cost ) nocons addtext(Demographic Controls, YES)

sleep 10

reg `rhs'  price_ben sub_ben  cost i.regiong [aw=`weight'],  vce(cluster state_fips)

outreg2 using StataGraphs/price_sub_reg_tract`rhs', append  tex(frag)  keep(price_ben sub_ben cost) nocons addtext(Demographic Controls, NO, REGION FE, YES)

sleep 10

reg `rhs'  price_ben sub_ben  cost CollPerc_t Pol_t i.regiong [aw=`weight'],  vce(cluster state_fips)

outreg2 using StataGraphs/price_sub_reg_tract`rhs', append  tex(frag)  keep(price_ben sub_ben cost) nocons addtext(Demographic Controls, YES, REGION FE, YES)

sleep 10

reg `rhs'  price_ben sub_ben  cost i.divisiong [aw=`weight'],  vce(cluster state_fips)

outreg2 using StataGraphs/price_sub_reg_tract`rhs', append  tex(frag)  keep(price_ben sub_ben cost) nocons addtext(Demographic Controls, NO, DIVISION FE, YES)

sleep 10

reg `rhs'  price_ben sub_ben  cost CollPerc_t Pol_t i.divisiong [aw=`weight'],  vce(cluster state_fips)

outreg2 using StataGraphs/price_sub_reg_tract`rhs', append  tex(frag)  keep(price_ben sub_ben cost) nocons addtext(Demographic Controls, YES, DIVISION FE, YES)

sleep 10

}
*/



*** save data for graphs

*export delimited  using ../Data/CleanedData/MergedData.csv, replace 

sort tract

gen region_t=regiong

foreach var in region_t {

	outsheet `var' using ../Data/CleanedData/`var'.csv, replace nonames
}


save ../Data/CleanedData/MergedData, replace


* state level

sum tot_ben unit_ben cost_ben

preserve



collapse   (mean) state_fips  (mean) price (mean) tot_ben (mean) cost (mean) unit_ben (mean) kwh_ben (mean) cost_ben (mean) price_ben  [aw=population], by(state_name)

gen sub_ben=unit_ben+kwh_ben+cost_ben

export delimited  using ../Data/CleanedData/StateAveSubs.csv, replace 

restore

collapse   (mean) regiong (mean) divisiong (mean) state_fips (mean) skwh (mean) scost (mean) sunit   (mean) A_t (mean) price (mean) tot_ben (mean) cost (mean) unit_ben (mean) kwh_ben (mean) cost_ben (mean) price_ben (sum) solar_system_count (sum) population (sum) BI_t (sum) number_of_panels_total, by(state_name)

gen install_perc=BI_t/number_of_panels_total 
gen install_perc2=solar_system_count/number_of_panels_total 
gen install_perc_pop=BI_t/population 

gen ptAt=price*A_t

gen linstall_perc=log(install_perc)
gen linstall_perc_pop=log(install_perc_pop)

gen sub_ben=unit_ben+cost_ben+kwh_ben

if `graphs'==1 {
foreach var in tot_ben price A_t sub_ben { // A_t tot_ben price ptAt  unit_ben  kwh_ben  cost_ben  price_ben install_perc2 {
	twoway (scatter  install_perc `var'  [aw=number_of_panels_total], mlabel(state_name) msymbol(circle_hollow) )(lfit  install_perc `var'), ///
graphregion(color(white)) xtitle("Benefits") ytitle("Installations") legend(off)

	graph export StataGraphs/StateInstall`var'.png, as(png) replace
	
	twoway (scatter  linstall_perc `var'  [aw=number_of_panels_total], mlabel(state_name) msymbol(circle_hollow) )(lfit  linstall_perc `var'), ///
graphregion(color(white)) xtitle("Benefits") ytitle("Log Installations") legend(off)

	graph export StataGraphs/StateLinstall`var'.png, as(png) replace	
	


	
}
}






gen tot_sub=kwh_ben  +cost_ben +unit_ben

sum tot_ben ptAt tot_sub

reg linstall_perc  price_ben kwh_ben cost cost_ben unit_ben  [aw=number_of_panels_total], r

reg linstall_perc_pop  price_ben kwh_ben cost cost_ben unit_ben  [aw=number_of_panels_total], r

reg linstall_perc_pop price_ben tot_sub cost  [aw=number_of_panels_total], r


save ../Data/CleanedData/StateAverages, replace

** data for summary table

local varlist  A_t price tot_ben tot_sub price_ben unit_ben kwh_ben cost_ben cost skwh scost sunit

foreach var of local varlist  {	
	qui sum `var' [aw=number_of_panels_total]
	local m_`var'=r(mean)
	local sd_`var'=r(sd)
	
	corr `var' install_perc_pop
	local rho_`var'=r(rho)
}






clear

local stats m sd rho

set obs 5

foreach var of local varlist {
	gen `var'=.
}

gen stat=""

local index=1

*pause on

foreach stat of local stats {
	replace stat="`stat'" if _n==`index'
	foreach var of local varlist {
	
		replace `var'=``stat'_`var'' if _n==`index'

	}
	
	local index=`index'+1
	display "`index'"
	*pause
}

save StataGraphs/state_sum_stats, replace


